package com.wzx.data.serviceimpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.data.dao.mapper.CourseMapper;
import com.wzx.data.entity.Course;
import com.wzx.data.entity.Subject;
import com.wzx.data.entity.Teacher;
import com.wzx.data.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.data.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程 服务层接口实现
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@Service
@Transactional
public class ICourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private ISubjectService subjectService;

    @Autowired
    private IVideoService videoService;

    @Autowired
    private IChapterService chapterService;

    //点播课程列表
    @Override
    public Map<String, Object> findPageCourse(Page<Course> pageParam, CourseVo vo) {
        //获取条件值
        String title = vo.getTitle();
        //二层分类
        String subjectId = vo.getSubjectId();
        //一层分类
        String subjectParentId = vo.getSubjectParentId();
        //讲师Id
        String teacherId = vo.getTeacherId();
        //判断条件为空，封装条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!ObjectUtil.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if(!ObjectUtil.isEmpty(subjectId)) {
            wrapper.eq("subject_id",subjectId);
        }
        if(!ObjectUtil.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!ObjectUtil.isEmpty(teacherId)) {
            wrapper.eq("teacher_id",teacherId);
        }

        //调用方法实现条件查询分页
        Page<Course> pages = baseMapper.selectPage(pageParam, wrapper);
        long totalCount = pages.getTotal();
        long totalPage = pages.getPages();
        List<Course> list = pages.getRecords();
        //查询数据里面有几个id
        //讲师id  课程分类id（一层 和 二层）
        //获取这些id对应名称，进行封装，最终显示
        //封装数据
        //遍历封装讲师和分类名称
        list.stream().forEach(item -> {
            this.getTeacherOrSubjectName(item);
        });
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",list);
        return map;
    }

    /**
     * 获取讲师和分类名称
     * @param course
     * @return
     */
    private Course getTeacherOrSubjectName(Course course) {
        //查询讲师名称
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher != null) {
            course.getParam().put("teacherName",teacher.getName());
        }
        //查询分类名称
        Subject subjectOne = subjectService.getById(course.getSubjectParentId());
        if(subjectOne != null) {
            course.getParam().put("subjectParentTitle",subjectOne.getName());
        }
        Subject subjectTwo = subjectService.getById(course.getSubjectId());
        if(subjectTwo != null) {
            course.getParam().put("subjectTitle",subjectTwo.getName());
        }
        return course;
    }
    /**
     * 添加课程基本信息
     * @param vo
     */
    @Override
    public void saveCourseInfo(CourseVo vo) {
        //添加课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(vo,course);
        baseMapper.insert(course);
    }

    /**
     * 根据id查询课程信息
     * @param id
     * @return
     */
    @Override
    public CourseVo getCourseInfoById(String id) {
        //课程基本信息
        Course course = baseMapper.selectById(id);
        if(course == null) {
            return null;
        }
        //封装
        CourseVo vo = new CourseVo();
        BeanUtils.copyProperties(course,vo);
        return vo;
    }

    /**
     * 修改课程信息
     * @param vo
     */
    @Override
    public void updateCourseId(CourseVo vo) {
        //修改课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(vo,course);
        this.updateById(course);
    }

    //根据课程id查询发布课程信息
    @Override
    public CourseVo getCoursePublishVo(String id) {
        return baseMapper.selectCourseVoById(id);
    }

    //课程最终发布
    @Override
    public void publishCourse(String id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1);//已经发布
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
    }

    //删除课程
    @Override
    public void removeCourseId(String id) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);

        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);

        //根据课程id删除课程
        baseMapper.deleteById(id);
    }

    //根据课程id查询课程详情
    @Override
    public Map<String, Object> getInfoById(String courseId) {
        //view_count浏览数量 +1
        Course course = baseMapper.selectById(courseId);
        course.setViewCount(course.getViewCount()+1);
        baseMapper.updateById(course);

        //根据课程id查询
        //课程详情数据
        CourseVo courseVo = baseMapper.selectCourseVoById(courseId);
        //课程章节小节数据
        List<ChapterVo> chapterVoList = chapterService.getTreeList(courseId);
        //课程描述信息

        //课程所属讲师信息
        Teacher teacher = teacherService.getById(course.getTeacherId());

        //封装map集合，返回
        Map<String,Object> map = new HashMap();
        map.put("courseVo", courseVo);
        map.put("chapterVoList", chapterVoList);
        map.put("description", null != course.getDescription() ? course.getDescription() : "");
        map.put("teacher", teacher);
        return map;
    }

    //查询所有课程
    @Override
    public List<Course> findlist() {
        List<Course> list = baseMapper.selectList(null);
        return list;
    }

}